package com.app.utils;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.main.SdUtilBase;
import com.core.lib.utils.main.StringUtilBase;
import com.eyzhs.app.R;

public class SdLocal {

	public static final String LEPU_FOLDER = "/lepu/confinement";
	public static String LEPU_PATH = "";

	public static String LEPU_PATH_SD = "";
	public static String DEFAULT_USER = "-1";

	/*
	 * 外置存储
	 */
	public static String getLepuRootPathSD(Context context) {
		if (StringUtilBase.stringIsEmpty(LEPU_PATH_SD)) {
			String path = SdUtilBase.getStorgePath(context);
			if (!StringUtilBase.stringIsEmpty(path)) {
				LEPU_PATH_SD = path + LEPU_FOLDER;
			}
		}
		return LEPU_PATH_SD;
	}

	// ==================================================================================
	/*
	 * 内置app存储
	 */
	public static String getLepuRootPath(Context context) {
		if (StringUtilBase.stringIsEmpty(LEPU_PATH)) {
			String path = context.getFilesDir().getAbsolutePath();
			// String path = SdUtilBase.getStorgePath(context);
			if (!StringUtilBase.stringIsEmpty(path)) {
				LEPU_PATH = path + LEPU_FOLDER;
			}
		}
		return LEPU_PATH;
	}

	// all user
	// 获取download folder
	public static String getDownloadFolder(Context context) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), "Download");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	// 获取temp folder
	public static String getTempFolder(Context context) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), "Temp");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}
	
	// 获取宝宝出生证 folder
	public static String getBabyFolder(Context context) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), "Baby");
		
		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	// =====================================================================================
	// user
	public static String getCacheDataFolder(Context context, String userId) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), userId + "/CacheData");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	public static String getCacheImageFolder(Context context) {
		String iconFolder = StringUtilBase.combinePath(
				getLepuRootPath(context), "/CacheImage");

		File file = new File(iconFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return iconFolder;
	}

	// 获取yuyin folder
	public static String getYuYinFolder(Context context, String userId) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), userId + "/YuYin");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	public static String getCacheImageFolder(Context context, String userId) {
		String iconFolder = StringUtilBase.combinePath(
				getLepuRootPath(context), userId + "/CacheImage");

		File file = new File(iconFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return iconFolder;
	}

	// ==================================================================
	// 获取相册 folder root path
	private static String getAlbumFolder(Context context, String userId) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPath(context), userId + "/MyAlbum");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	public static String getAlbumUserFolder(Context context, String userId) {
		String fileFolder = StringUtilBase.combinePath(
				getAlbumFolder(context, userId), "UserPhoto");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	public static String getAlbumHotPhotoFolder(Context context, String albumId) {
		String fileFolder = StringUtilBase.combinePath(
				getAlbumFolder(context, albumId), "HotPhoto");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}
	
	// =====================================================================================

	public static String getCacheImagePath(Context context, String flag) {
		return StringUtilBase.combinePath(getCacheImageFolder(context), flag
				+ ".jpg");
	}

	public static String getUserPath(Context context, String userId) {
		return StringUtilBase.combinePath(getTempFolder(context), userId
				+ ".jpg");
	}

	public static String getAppIconPath(Context context) {
		String appIconPath = StringUtilBase.combinePath(
				getTempFolder(context), "app_icon" + ".png");
		if (!new File(appIconPath).exists()) {
			Bitmap iconBitmap = ImageUtilBase.getBitmapByResId(context,
					R.drawable.app_icon);
			ImageUtilBase.saveImageToSd(CompressFormat.PNG,iconBitmap, appIconPath);
		}
		return appIconPath;
	}

	public static String getCacheImagePath(Context context, String userId,
			String flag) {
		return StringUtilBase.combinePath(getCacheImageFolder(context, userId),
				flag + ".jpg");
	}

	public static String getAlbumUserPath(Context context, String userId,
			String flag) {
		return StringUtilBase.combinePath(getAlbumUserFolder(context, userId),
				flag + ".jpg");
	}

	/*
	 * 只放热门相册下载的图片
	 */
	public static String getAlbumHotPath(Context context, String userId,
			String flag) {
		return StringUtilBase.combinePath(
				getAlbumHotPhotoFolder(context, userId), flag + ".jpg");
	}

	public static String getTempPhotoPath(Context context, String flag) {
		return StringUtilBase
				.combinePath(getTempFolder(context), flag + ".jpg");
	}

	public static String getTempFilePath(Context context, String flag) {
		return StringUtilBase
				.combinePath(getTempFolder(context), flag + ".txt");
	}

	public static String getDebugLogPath(Context context) {
		return StringUtilBase.combinePath(getTempFolder(context), "debug.log");
	}

	public static String getDatabasePath(Context context, String dbName) {
		return context.getDatabasePath(dbName).getPath();
	}

	public static String getDownloadApkPath(Context context, String path) {
		return StringUtilBase.combinePath(getDownloadFolder(context), path
				+ ".apk");
	}

	// 获取头像 缓存folder
	public static String getHeadIconFolder(Context context) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), "HeadIcon");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}
}
